package com.gitee.grower.core.kether

import ac.github.oa.internal.core.item.ItemInstance
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import taboolib.common.platform.function.adaptPlayer
import taboolib.common.platform.function.submitAsync
import taboolib.common5.Coerce
import taboolib.module.kether.*

val namespaceItem = listOf("item-grower")

fun ScriptFrame.itemStack(): ItemStack {
    return variables().get<Any?>("@itemStack").orElse(null) as? ItemStack ?: error("No item selected.")
}

fun ScriptFrame.itemInstance(): ItemInstance {
    if (!variables().keys().contains("@itemInstance")) {
        val itemInstance = ItemInstance.get(itemStack()) ?: error("Item type no 'OriginAttribute'")
        variables()["@itemInstance"] = itemInstance
        return itemInstance
    }
    return variables().get<Any?>("@itemInstance").get() as ItemInstance
}

fun toKetherBool(player: Player, parse: String, itemStack: ItemStack): Boolean {
    if (parse == "true") return true
    return Coerce.toBoolean(toKether(player, parse, itemStack))
}

fun toKetherBool(player: Player, parse: String, block: ScriptContext.() -> Unit = {}): Boolean? {
    if (parse == "true") return true
    return Coerce.toBoolean(toKether(player, parse, block))
}

fun toKetherFunction(player: Player, parse: String, itemStack: ItemStack): String {
    return toKetherFunction(player, parse) {
        this.rootFrame().variables()["@itemStack"] = itemStack
    }
}

fun toKetherFunction(player: Player, parse: String, block: ScriptContext.() -> Unit = {}): String {
    return runKether {
        KetherFunction.parse(parse, sender = adaptPlayer(player), namespace = namespaceItem) { block(this) }
    } ?: parse
}

fun toKetherInt(player: Player, parse: String,itemStack: ItemStack): Int {
    return Coerce.toInteger(toKether(player, parse, itemStack) ?: -1)
}

fun toKetherInt(player: Player, parse: String,block: ScriptContext.() -> Unit = {}): Int {
    return Coerce.toInteger(toKether(player, parse, block) ?: -1)
}

fun toKether(player: Player,parse: String,itemStack: ItemStack) : Any? {
    return toKether(player,parse) {
        this.rootFrame().variables()["@itemStack"] = itemStack
    }
}

fun toKether(player: Player,parse: String,block: ScriptContext.() -> Unit = {}) : Any? {
    return runKether {
        KetherShell.eval(parse, sender = adaptPlayer(player), namespace = namespaceItem) {
            block(this)
        }.get()
    }
}

fun toKetherDouble(player: Player, parse: String, itemStack: ItemStack): Double {
    return Coerce.toDouble(toKether(player, parse, itemStack) ?: 0.0)
}

fun toKetherDouble(player: Player, parse: String, block: ScriptContext.() -> Unit = {}): Double {
    return Coerce.toDouble(toKether(player, parse, block) ?: 0.0)
}

fun playKether(player: Player,script: String,itemStack: ItemStack) {
    playKether(player, script) {
        this.rootFrame().variables()["@itemStack"] = itemStack
    }
}

fun playKether(player: Player,script: String,block: ScriptContext.() -> Unit = {}) {
    submitAsync {
        KetherShell.eval(script, sender = adaptPlayer(player), namespace = namespaceItem) {
            block(this)
        }
    }
}